package com.ccjk.beusoft.thirdlib.plugin2.hook.proxy;

import android.content.ComponentCallbacks;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.ccjk.beusoft.thirdlib.StubActivity;
import com.ccjk.beusoft.thirdlib.app.App;
import com.ccjk.beusoft.thirdlib.plugin2.PluginManager2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by CCJK on 2017/12/22.
 */

public class AmsProxy implements InvocationHandler {
    private static final String TAG = "AMS_PROXY";
    private Object origin;

    public AmsProxy(Object origin) {
        this.origin = origin;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (TextUtils.equals(method.getName(), "startActivity")) {
            try {
                Intent rawIntent = (Intent) args[2];
                if (PluginManager2.get().isPlugin(rawIntent.getPackage())) {
                    Intent newIntent = new Intent();
                    newIntent.setClass(PluginManager2.get().getHostContext(), StubActivity.class);
                    newIntent.putExtra(PluginManager2.KEY_PLUGIN_INTENT, rawIntent);
                    args[2] = newIntent;
                }
            } catch (Exception e) {
                Log.e(TAG, method.getName(), e);
            }
        }
        return method.invoke(origin, args);
    }
}
